﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace jmwqz
{
    public partial class product_add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string strErr = "";

            if (this.txtproductName.Text.Trim().Length == 0)
            {
                strErr += "商品名称不能为空！\\n";
            }

            if (!MyCommFun.isNumber(txtstock.Text))
            {
                strErr += "库存格式错误！\\n";
            }

            bool upselling = true;//上架
            if (radType.SelectedItem.Value == "2")
            {
                upselling = false;
            }
            int stock = int.Parse(this.txtstock.Text);

            DateTime addDate = DateTime.Now;

            decimal salePrice = decimal.Parse(this.txtsalePrice.Text);

            string productName = this.txtproductName.Text;
            string shortDesc = this.txtshortDesc.Text;

            #region 保存相册====================
            //检查是否有自定义图片
            string thumb_path = "";
            string original_path = "";
            string[] albumArr = Request.Form.GetValues("hid_photo_name");
            string[] remarkArr = Request.Form.GetValues("hid_photo_remark");
            if (albumArr != null && albumArr.Length > 0)
            {
                //List<Model.wx_shop_albums> ls = new List<Model.wx_shop_albums>();
                for (int i = 0; i < albumArr.Length; i++)
                {
                    string[] imgArr = albumArr[i].Split('|');
                    if (imgArr.Length == 3)
                    {
                        original_path = imgArr[1];
                        thumb_path = imgArr[2];
                        break;
                    }
                }
                //model.albums = ls;
            }

            Maticsoft.Model.tb_item model = new Maticsoft.Model.tb_item();
            model.name = productName;
            model.description = shortDesc;
            model.summary = shortDesc;
            model.stock = stock;
            model.price = salePrice;
            model.is_sale = upselling ? 1:0;
            model.img_thumb = thumb_path;
            model.img_orgin = original_path;
            #endregion


            Maticsoft.BLL.tb_item bll = new Maticsoft.BLL.tb_item();
            if (bll.Add(model))
            {
                JscriptMsg("***商品录入成功,请继续录入***！", "product_add.aspx", "Success");
            }
            else
            {
                JscriptMsg("***商品录入成功***！", "", "Success");
            }
        }


        /// <summary>
        /// 添加编辑删除提示
        /// </summary>
        /// <param name="msgtitle">提示文字</param>
        /// <param name="url">返回地址</param>
        /// <param name="msgcss">CSS样式</param>
        protected void JscriptMsg(string msgtitle, string url, string msgcss)
        {
            string msbox = "parent.jsprint(\"" + msgtitle + "\", \"" + url + "\", \"" + msgcss + "\")";
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", msbox, true);
        }
    }
}